package com.itboye.mengwu.testpic.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.ui.aiwujia.ActivityAiwujiaMomPublish;
import com.itboye.mengwu.app.ui.aiwujia.ActivityAiwujiaYuErjingPublish;
import com.itboye.mengwu.app.ui.aiwujia.ActivityBaoBaoLePublish;

public class PhotoActivity extends BaseActivity {

	private ArrayList<View> listViews = null;
	private ViewPager pager;
	private MyPageAdapter adapter;
	private int count;

	public List<Bitmap> bmp = new ArrayList<Bitmap>();
	public List<String> drr = new ArrayList<String>();
	public List<String> del = new ArrayList<String>();
	public int max;
	int position;
	public String type;
	private ArrayList<String> arrayList = new ArrayList<>();
	RelativeLayout photo_relativeLayout;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_photo;
	}

	@Override
	public void onMyCreate() {
		arrayList = getIntent().getStringArrayListExtra("imgUrls");
		photo_relativeLayout = (RelativeLayout) findViewById(R.id.photo_relativeLayout);
		photo_relativeLayout.setBackgroundColor(0x70000000);
		position = getIntent().getIntExtra("ID", 0);
		type=getIntent().getStringExtra("type");
		Button photo_bt_exit = (Button) findViewById(R.id.photo_bt_exit);
		photo_bt_exit.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.putExtra("arrayList", arrayList);
				setResult(102, intent);
				finish();
			}
		});
		Button photo_bt_del = (Button) findViewById(R.id.photo_bt_del);
		photo_bt_del.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ByAlert.alert(position+"");
				if(position==1){
					arrayList.remove(0);
					listViews.remove(0);
					if(type.equals("aiwujia"))
					ActivityAiwujiaMomPublish.imgUrls.remove(0);
					else if(type.equals("yuerjing"))
						ActivityAiwujiaYuErjingPublish.imgUrls.remove(0);
					else if(type.equals("baobaole"))
						ActivityBaoBaoLePublish.imgUrls.remove(0);
				}else{
				arrayList.remove(position);
				listViews.remove(position);
				if(type.equals("aiwujia"))
				ActivityAiwujiaMomPublish.imgUrls.remove(position);
				else if(type.equals("yuerjing"))
					ActivityAiwujiaYuErjingPublish.imgUrls.remove(position);
				else if(type.equals("baobaole"))
					ActivityBaoBaoLePublish.imgUrls.remove(position);
				else if(type.equals("myDongTai"))
					PublishedActivity.imgUrls.remove(position);
				else if(type.equals("baobaole"))
					ActivityBaoBaoLePublish.imgUrls.remove(position);
				else if(type.equals("wodeTieZi"))
					ActivityBaoBaoLePublish.imgUrls.remove(position);

				}

				if (arrayList.size() == 0) {
					Intent intent=new Intent();
					intent.putExtra("arrayList", arrayList);
					setResult(102, intent);
					finish();
				}else{
				adapter = new MyPageAdapter(listViews);// 构造adapter
				pager.setAdapter(adapter);// 设置适配器
//				if(position<arrayList.size())
				try {
					pager.setCurrentItem(position);
				} catch (Exception e) {
					// TODO: handle exception
//					pager.setCurrentItem(position);
					setResult(102);
					finish();
				}}
			}
		});
		Button photo_bt_enter = (Button) findViewById(R.id.photo_bt_enter);
		photo_bt_enter.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				// Bimp.bmp = bmp;
				// Bimp.drr = drr;
				// Bimp.max = max;
				// for (int i = 0; i < del.size(); i++) {
				// FileUtils.delFile(del.get(i) + ".JPEG");
				// }
				// finish();
			}
		});

		pager = (ViewPager) findViewById(R.id.viewpager);
		pager.addOnPageChangeListener(pageChangeListener);
		// http://blog.sina.com.cn/s/blog_7607703f0101a7t0.html.setOnPageChangeListener(pageChangeListener);
		for (int i = 0; i < arrayList.size(); i++) {
			initListViews(arrayList.get(i));//
		}

		adapter = new MyPageAdapter(listViews);// 构造adapter
		pager.setAdapter(adapter);// 设置适配器
		Intent intent = getIntent();
		int id = intent.getIntExtra("ID", 0);
		pager.setCurrentItem(id);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		
		if(keyCode==KeyEvent.KEYCODE_BACK){
			setResult(102);
		}
		return super.onKeyDown(keyCode, event);
	}

	/*
	 * public void onCreate(Bundle savedInstanceState) {
	 * super.onCreate(savedInstanceState);
	 * setContentView(R.layout.activity_photo);
	 * 
	 * 
	 * }
	 */

	private void initListViews(String url) {
		if (listViews == null)
			listViews = new ArrayList<View>();
		ImageView img = new ImageView(this);// 构造textView对象
		img.setBackgroundColor(0xff000000);
		XImageLoader.load("file://" + url, img);
		img.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		listViews.add(img);// 添加view
	}

	private OnPageChangeListener pageChangeListener = new OnPageChangeListener() {

		public void onPageSelected(int arg0) {// 页面选择响应函数
			position = arg0;
			Log.v("PAGE_no", position+"");
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {// 滑动中。。。

		}

		public void onPageScrollStateChanged(int arg0) {// 滑动状态改变

		}
	};

	class MyPageAdapter extends PagerAdapter {

		private ArrayList<View> listViews;// content

		private int size;// 页数

		public MyPageAdapter(ArrayList<View> listViews) {// 构造函数
															// 初始化viewpager的时候给的一个页面
			this.listViews = listViews;
			size = listViews == null ? 0 : listViews.size();
		}

		public void setListViews(ArrayList<View> listViews) {// 自己写的一个方法用来添加数据
			this.listViews = listViews;
			size = listViews == null ? 0 : listViews.size();
		}

		@Override
		public int getCount() {// 返回数量
			return size;
		}

		@Override
		public int getItemPosition(Object object) {
			return POSITION_NONE;
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {//
//			if(listViews.size()==arg1% size){
//				finish();
//				
//			}
		// 销毁view对象
			try {
				((ViewPager) arg0).removeView(listViews.get(arg1 % size));
			} catch (Exception e) {
			}
		}

		public void finishUpdate(View arg0) {
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {// 返回view对象
			try {
				((ViewPager) arg0).addView(listViews.get(arg1 % size), 0);

			} catch (Exception e) {
			}
			return listViews.get(arg1 % size);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

	}

}
